package com.asa.tongji;

import java.util.Arrays;

import com.asa.tongji.bean.FenXiBaogao;
import com.asa.tongji.bean.XiangXianTu;
import com.asa.tongji.bean.ZhiFangTu;
/**
 * 将工具类
 * @author Administrator
 *
 */
public class TongJiFenXi {

	/**
	 * 完整的分销，还可以往后加
	 * @param x
	 * @param n 将分析出来的直方图分成多少份
	 */
	public static FenXiBaogao jichufenxi(double[] x,int n){
		
		double average = TUtils1.average(x);
		double variance = TUtils1.variance(x);
		XiangXianTu xiangxiantu = TUtils1.xiangxiantu(x);
		ZhiFangTu zhifangtu = TUtils1.zhifangtu(x, n);
		
		
		FenXiBaogao fenXiBaogao = new FenXiBaogao();
		fenXiBaogao.setAverage(average);
		fenXiBaogao.setVariance(variance);
		fenXiBaogao.setXiangXianTu(xiangxiantu);
		fenXiBaogao.setZhiFangTu(zhifangtu);
		
		return fenXiBaogao;
	}
	
	
	/**
	 * 判断样本间存不存在相关关系，如果存在是怎样的线性关系
	 * @param x
	 * @param y
	 * @throws Exception
	 * 
	 * double[] x = {12.5,15.3,23.2,26.4,33.5,34.4,39.4,45.2,55.4,60.9};
		double[] y = {21.2,23.9,32.9,34.1,42.5,43.2,49,52.8,59.4,63.5};
		
		//System.out.println(x.length+"      "+y.length);
		xiangguangfenxi(x, y);
	 */
	public static void xiangguangfenxi(double[] x, double[] y) throws Exception{
		double covariance = TUtils1.covariance(x, y);//协方差
		double correlationCoefficient = TUtils1.correlationCoefficient(x, y);//相关系数
		boolean xiangguanxishujianyan = TUtils4.xiangguanxishujianyan(0.95, x.length, correlationCoefficient);//相关信检验
		double[] xianxinghuigui = TUtils4.xianxinghuigui(x, y);//线性拟合函数的参数a和b
		boolean huiguixishuxianzhuxingjianyanb = TUtils4.huiguixishuxianzhuxingjianyanb(x, y, 0.95);
		boolean huiguixishuxianzhuxingjianyana = TUtils4.huiguixishuxianzhuxingjianyana(x, y, 0.95);
		
		//System.out.println(covariance);
		//System.out.println(correlationCoefficient);
		//System.out.println(xiangguanxishujianyan);
		//System.out.println("y = "+xianxinghuigui[1]+"*x + "+xianxinghuigui[0]);
		//System.out.println(huiguixishuxianzhuxingjianyanb);
		//System.out.println(huiguixishuxianzhuxingjianyana);
		
	}

	
	
	/**
	 * 对单组数据进行分析，测评,确立估值的范围
	 * @param x	样本值
	 * @param xian	样本筛选区分分隔线
	 * @param a	置信度
	 * @throws Exception
	 */

	public static void shujuguzhifenxi(double [] x,double xian,double a) throws Exception{
		int n = 0;
		for (int i = 0; i < x.length; i++) {
			if (x[i]>xian) {
				n++;
			}
		}
		
		double[] zhixingqujian1 = TUtils2.zhixingqujian(x.length, n);//比例区间
		//System.out.println(Arrays.toString(zhixingqujian1));
		double[] zongtijunzhizhixingqujian2 = TUtils2.zongtijunzhizhixingqujian2(x, a);//均值区间
		//System.out.println(Arrays.toString(zongtijunzhizhixingqujian2));
		double variance = TUtils1.variance(x);
		double[] zongtifangcha = TUtils2.zongtifangcha(a, n, variance);//总体方差
		//System.out.println(Arrays.toString(zongtifangcha));
	}
	
	
	/**
	 * 样本之间对比，测评
	 * 默认不是全样本空间吧，
	 * @throws Exception 
	 * @param x
	 * @param y
	 * @param t
	 * @param xian
	 * @throws Exception
	 */

	public static void yangbenduibishujuguzhifenxi (double[] x, double[] y,double t,double xian) throws Exception{
		
		double ux = TUtils1.average(x);
		double uy = TUtils1.average(y);
		double ox = Math.sqrt(TUtils1.variance(x));
		double oy = Math.sqrt(TUtils1.variance(y));
		
		//均值比较,范围
		double[] liangzhengtaiduibi = TUtils3.liangzhengtaiduibi3(ux, uy, ox, oy, x.length, y.length, t);
		//System.out.println("符合标准正态分布的均值比较:"+Arrays.toString(liangzhengtaiduibi));
		
		//不符合标准正态分布的均值比较
		double[] feiliangzhengtaiduibi = TUtils3.feiliangzhengtaiduibi(ux, uy, ox, oy, x.length, y.length, t);
		//System.out.println("不符合标准正态分布的均值比较:"+Arrays.toString(feiliangzhengtaiduibi));
		
		double nx = 0;
		double ny = 0;
		for (int i = 0; i < x.length; i++) {
			if (x[i]>xian) {
				nx++;
			}
		}
		for (int i = 0; i < y.length; i++) {
			if (y[i]>xian) {
				ny++;
			}
		}
		
		//总体比例比较
		double[] liangzhengtaibilieduibi = TUtils3.liangzhengtaibilieduibi(nx/x.length, ny/y.length, t, x.length, y.length);
		//System.out.println("总体比例比较"+Arrays.toString(liangzhengtaibilieduibi));
		
		
		//两个独立正态总体的总统方差比较
		double[] liangzhengtaifangchaduibi = TUtils3.liangzhengtaifangchaduibi(x.length, y.length, ox, oy, t);
		//System.out.println("两个独立正态总体的总统方差比较:"+Arrays.toString(liangzhengtaifangchaduibi));
		
	}
	
	/**
	 * 根据样本，使用零假设来对样本申明（包括比例，均值，方差）进行检验，
	 * @param x
	 * @param p
	 * @param ux
	 * @param a
	 * @param ao
	 * @param xian
	 * @throws Exception
	 */
	public static void dangxijianyanfenxi(double[] x,double p,double ux,double a,double ao,double xian) throws Exception{
		double p1 = 0;
		for (int i = 0; i < x.length; i++) {
			if (x[i]<xian) {
				p1++;
			}
		}
		p1 = p1/x.length;
		//总体比例假设检验
		boolean jiashejianyan = TUtils4.jiashejianyan(p, p1, x.length, a);
		//System.out.println("总体比例假设检验:"+jiashejianyan);
		
		
		double o = TUtils1.variance(x);
		double x1 = TUtils1.average(x);
		
		//总体均值的假设检验,总体方差未知的情况下
		boolean junzhijianyanwuzongtifangcha = TUtils4.junzhijianyanwuzongtifangcha(o, ux, x1, x.length, a);
		//System.out.println("总体均值的假设检验,总体方差未知的情况下:"+junzhijianyanwuzongtifangcha);
		
		
		//总体方差的假设检验
		boolean fangchajianyan = TUtils4.fangchajianyan(x.length, ao, o, a);
		//System.out.println("总体方差的假设检验:"+fangchajianyan);
		
	}
	
	
	public static void asa(double[] x,double[] y,double p1,double p2,double u1,double u2,double a,double xian){
		
		int x1 = 0;
		int y1 = 0;
		
		for (int i = 0; i < x.length; i++) {
			if (x[i]<xian) {
				x1++;
			}
		}
		
		for (int i = 0; i < y.length; i++) {
			if (y[i]<xian) {
				y1++;
			}
		}
		
		//检验两个总体比例是否相等
		boolean zongtibilijianyan = TUtils4.zongtibilijianyan(x1, y1, p1, p2, x.length, y.length, a);
		//System.out.println("检验两个总体比例是否相等:"+zongtibilijianyan);
		
		
		
		double avx1 = TUtils1.average(x);
		double avx2 = TUtils1.average(y);
		double o1 = TUtils1.variance(x);
		double o2 = TUtils1.variance(y);
		//两个独立总体均值的比较,比较均值是否有明显差别
		boolean liangjunzhijianyan3 = TUtils4.liangjunzhijianyan3(avx1,avx2, u1, u2, x.length, y.length, o1, o2, a);
		//System.out.println("两个独立总体均值的比较:"+liangjunzhijianyan3);
		
		
	 	
		
		
		
		
	}
	
	
	
	
	
	
	
	
	public static void main(String[] args) throws Exception{
		double[] a = {1,2,9,4,5,6,7,8};
		ZhiFangTu zhifangtu = TUtils1.zhifangtu(a , 2);
		//System.out.println(zhifangtu);
		
		
	}
	
	
}
